//
//  UserInfoViewController.m
//  BlueTDevice
//
//  Created by MingmingSun on 16/9/4.
//  Copyright © 2016年 Sunmingming. All rights reserved.
//

#import "UserInfoViewController.h"


@interface UserInfoViewController ()<UIAlertViewDelegate>

@property(nonatomic,strong) SpUserItem *cacheUserItem;
@property(nonatomic,assign) int alertTag;

@end

@implementation UserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人信息";
    self.cacheUserItem = APPALL.myUserItem;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(savePressed:)];
}

-(void)savePressed:(id)sender{
    if(!_cacheUserItem.nickname.length){
        UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"请正确填写您的昵称！" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [vc addAction:cancelAction];
        [self presentViewController:vc animated:YES completion:nil];
        return;
    }
    [SVProgressHUD showWithStatus:@"loading..."];
    NSDictionary *PA = @{@"age": self.cacheUserItem.age,
                         @"height":self.cacheUserItem.height,
                         @"nickname":self.cacheUserItem.nickname,
                         @"phone":self.cacheUserItem.phone,
                         @"weight":self.cacheUserItem.weight};
    [APPALL.netSession POST:[NSString stringWithFormat:@"%@%@",APIURL,APIUserInfo]
                 parameters:PA
                   progress:nil
                    success:^(NSURLSessionDataTask * task, id responseObject){
                        NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:   NSJSONReadingMutableContainers error:nil];
                        NSLog(@"JSON: %@", JSON);
                        if([JSON[@"add_personal_information"] isEqualToString:@"success"]){
                            [SVProgressHUD dismiss];
                            APPALL.myUserItem = _cacheUserItem;
                            [APPALL.myUserItem saveToDB];
                            [self.navigationController popViewControllerAnimated:YES];
                        }else{
                            [SVProgressHUD showErrorWithStatus:@"更新失败"];
                        }
                    }
                    failure:^(NSURLSessionDataTask * task, NSError * error){
                        NSLog(@"%@",error);
                        [SVProgressHUD dismiss];
                        //[SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"请求失败:%@", error.description]];
                    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 3;
        case 1:
            return 3;
        default:
            return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = @"characteristicCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                      reuseIdentifier:cellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:{
                    cell.textLabel.text = @"头像";
                }
                    break;
                case 1:{
                    cell.textLabel.text = @"昵称";
                    cell.detailTextLabel.text = self.cacheUserItem.nickname;
                }
                    break;
                case 2:{
                    cell.textLabel.text = @"电话";
                    cell.detailTextLabel.text = self.cacheUserItem.phone;
                }
                    break;
                default:
                    break;
            }
            break;
        case 1:
            switch (indexPath.row) {
                case 0:{
                    cell.textLabel.text = @"身高/cm";
                    cell.detailTextLabel.text = self.cacheUserItem.height;
                }
                    break;
                case 1:{
                    cell.textLabel.text = @"体重/kg";
                    cell.detailTextLabel.text = self.cacheUserItem.weight;
                }
                    break;
                case 2:{
                    cell.textLabel.text = @"年龄/岁";
                    cell.detailTextLabel.text = self.cacheUserItem.age;
                }
                    break;
                default:
                    break;
            }
            break;
        default:
            break;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:{
                }
                    break;
                case 1:{
                    UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"请输入您的昵称" message:@"" preferredStyle:UIAlertControllerStyleAlert];
                    [vc addTextFieldWithConfigurationHandler:^(UITextField *textField){
                        textField.placeholder = @"";
                    }];
                    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的"style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
                        UITextField *login = vc.textFields.firstObject;
                        NSLog(@"%@",login.text);
                        if(login.text.length){
                            self.cacheUserItem.nickname = login.text;
                            [self.tableView reloadData];
                        }
                    }];
                    [vc addAction:cancelAction];
                    [vc addAction:okAction];
                    [self presentViewController:vc animated:YES completion:nil];
                }
                    break;
                case 2:{
                    UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"请输入您的电话" message:@"" preferredStyle:UIAlertControllerStyleAlert];
                    [vc addTextFieldWithConfigurationHandler:^(UITextField *textField){
                        textField.placeholder = @"";
                        textField.keyboardType = UIKeyboardTypeNumberPad;
                    }];
                    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的"style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
                        UITextField *login = vc.textFields.firstObject;
                        NSLog(@"%@",login.text);
                        if(login.text.length){
                            self.cacheUserItem.phone = login.text;
                            [self.tableView reloadData];
                        }
                    }];
                    [vc addAction:cancelAction];
                    [vc addAction:okAction];
                    [self presentViewController:vc animated:YES completion:nil];
                }
                    break;
                default:
                    break;
            }
            break;
        case 1:
            switch (indexPath.row) {
                case 0:{
                    UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"请输入您的身高" message:@"" preferredStyle:UIAlertControllerStyleAlert];
                    [vc addTextFieldWithConfigurationHandler:^(UITextField *textField){
                        textField.placeholder = @"";
                        textField.keyboardType = UIKeyboardTypeNumberPad;
                    }];
                    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的"style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
                        UITextField *login = vc.textFields.firstObject;
                        NSLog(@"%@",login.text);
                        if(login.text.length){
                            self.cacheUserItem.height = login.text;
                            [self.tableView reloadData];
                        }
                    }];
                    [vc addAction:cancelAction];
                    [vc addAction:okAction];
                    [self presentViewController:vc animated:YES completion:nil];
                }
                    break;
                case 1:{
                    UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"请输入您的体重" message:@"" preferredStyle:UIAlertControllerStyleAlert];
                    [vc addTextFieldWithConfigurationHandler:^(UITextField *textField){
                        textField.placeholder = @"";
                        textField.keyboardType = UIKeyboardTypeNumberPad;
                    }];
                    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的"style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
                        UITextField *login = vc.textFields.firstObject;
                        NSLog(@"%@",login.text);
                        if(login.text.length){
                            self.cacheUserItem.weight = login.text;
                            [self.tableView reloadData];
                        }
                    }];
                    [vc addAction:cancelAction];
                    [vc addAction:okAction];
                    [self presentViewController:vc animated:YES completion:nil];
                }
                    break;
                case 2:{
                    UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"请输入您的年龄" message:@"" preferredStyle:UIAlertControllerStyleAlert];
                    [vc addTextFieldWithConfigurationHandler:^(UITextField *textField){
                        textField.placeholder = @"";
                        textField.keyboardType = UIKeyboardTypeNumberPad;
                    }];
                    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的"style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
                        UITextField *login = vc.textFields.firstObject;
                        NSLog(@"%@",login.text);
                        if(login.text.length){
                            self.cacheUserItem.age = login.text;
                            [self.tableView reloadData];
                        }
                    }];
                    [vc addAction:cancelAction];
                    [vc addAction:okAction];
                    [self presentViewController:vc animated:YES completion:nil];
                }
                    break;
                default:
                    break;
            }
            break;
        default:
            break;
    }
}

@end
